package com.cleaningcloud.erp.service.impl;

import com.cleaningcloud.common.core.utils.DateUtils;
import com.cleaningcloud.common.core.utils.UniqueIdUtils;
import com.cleaningcloud.common.security.utils.CommunityTypeConversionUtils;
import com.cleaningcloud.common.security.utils.SecurityUtils;
import com.cleaningcloud.erp.api.domain.ErpWareCommodityNumber;
import com.cleaningcloud.erp.api.domain.ErpWareHouse;
import com.cleaningcloud.erp.mapper.ErpWareCommodityNumberMapper;
import com.cleaningcloud.erp.mapper.ErpWareHouseMapper;
import com.cleaningcloud.erp.service.IErpWareHouseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 仓库管理Service业务层处理
 *
 * @author cleaningcloud
 * @date 2024-09-26
 */
@Service
public class ErpWareHouseServiceImpl implements IErpWareHouseService {
    @Autowired
    private ErpWareHouseMapper erpWareHouseMapper;

    @Autowired
    private ErpWareCommodityNumberMapper erpWareCommodityNumberMapper;

    /**
     * 查询仓库管理
     *
     * @param wareId 仓库管理主键
     * @return 仓库管理
     */
    @Override
    public ErpWareHouse selectErpWareHouseByWareId(Long wareId) {
        return erpWareHouseMapper.selectErpWareHouseByWareId(wareId);
    }

    @Override
    public ErpWareHouse selectErpWareHouseByProjectId(Long projectId) {
        return erpWareHouseMapper.getErpWareHouseByProjectId(projectId);
    }

    @Override
    public ErpWareHouse selectErpWareHouseByPostId(Long postId) {
        return erpWareHouseMapper.selectErpWareHouseByPostId(postId);
    }

    /**
     * 查询仓库管理列表
     *
     * @param erpWareHouse 仓库管理
     * @return 仓库管理
     */
    @Override
    public List<ErpWareHouse> selectErpWareHouseList(ErpWareHouse erpWareHouse) {
        erpWareHouse.getParams().putIfAbsent("notIdentifier", "1");
        return erpWareHouseMapper.selectErpWareHouseList(erpWareHouse);
    }

    /**
     * 新增仓库管理
     *
     * @param erpWareHouse 仓库管理
     * @return 结果
     */
    @Override
    @Transactional
    public int insertErpWareHouse(ErpWareHouse erpWareHouse) {
        erpWareHouse.setCreateTime(DateUtils.getNowDate());
        erpWareHouse.setFlag("Y");
        erpWareHouse.setCreateId(SecurityUtils.getUserId());
        erpWareHouse.setCreateName(SecurityUtils.getUsername());
        if (erpWareHouse.getProjectId() != null && erpWareHouse.getProjectId() > 0) {
            if (erpWareHouse.getWareType() != null && erpWareHouse.getWareType().equals(1L)) {
                //查询胜博仓Id
                ErpWareHouse symbolWareHouse = erpWareHouseMapper.getErpWareHouseSymbol();
                if (symbolWareHouse == null) {
                    throw new RuntimeException("生成项目仓失败，请先初始化胜博仓！");
                }
                erpWareHouse.setWarePid(symbolWareHouse.getWareId());
            }
            erpWareHouse.setIdentifier("projectWareHouse");
            erpWareHouse.setStaffId(erpWareHouse.getStaffId());
            erpWareHouse.setWareStatus(1L);
            erpWareHouse.setWareNo("CK" + UniqueIdUtils.generateUniqueId());
            //根据项目Id查询项目对应仓库
            ErpWareHouse projectWareHouse = erpWareHouseMapper.getErpWareHouseByProjectId(erpWareHouse.getProjectId());
            if (projectWareHouse == null && erpWareHouse.getWareType().equals(1L)) {
                erpWareHouseMapper.insertErpWareHouse(erpWareHouse);
            }
            if (erpWareHouse.getPostIdList() != null && erpWareHouse.getPostIdList().size() > 0) {

                //根据项目Id查询项目对应仓库
                projectWareHouse = erpWareHouseMapper.getErpWareHouseByProjectId(erpWareHouse.getProjectId());
                if (projectWareHouse == null) {
                    throw new RuntimeException("生成个人仓失败，项目仓未创建！");
                }
                erpWareHouse.setWarePid(projectWareHouse.getWareId());
                erpWareHouse.setWareType(2L);

                //生成个人仓库数量
                List<ErpWareHouse> erpWareHouseList = erpWareHouseMapper.getPersonalErpWareHouseListByProjectId(erpWareHouse.getProjectId());
                if (erpWareHouseList == null) {
                    erpWareHouseList = new ArrayList<>();
                }
                List<ErpWareHouse> staffList = new ArrayList<>();
                ErpWareHouse wareHouse = null;
                List<ErpWareHouse> exitErpWareHouseList = new ArrayList<>();
                for (Long postId : erpWareHouse.getPostIdList()) {
                    boolean postExit = false;
                    for (ErpWareHouse house : erpWareHouseList) {
                        if (house.getStaffId() != null && house.getStaffId() > 0) {
                            if (postId.equals(house.getStaffId())) {
                                postExit = true;
                                house.setWareStatus(1L);//强制更改为启用
                                exitErpWareHouseList.add(house);
                            }
                        }
                    }
                    if (!postExit) {
                        wareHouse = new ErpWareHouse();
                        BeanUtils.copyProperties(projectWareHouse, wareHouse);
                        wareHouse.setWareNo("CK" + UniqueIdUtils.generateUniqueId() + "-" + postId);
                        wareHouse.setWareType(2L);
                        wareHouse.setWareName(projectWareHouse.getWareName() + "-" + UniqueIdUtils.generateUniqueId() + "-" + postId);
                        wareHouse.setWarePid(null);
                        wareHouse.setWarePid(projectWareHouse.getWareId());
                        wareHouse.setWareStatus(1L);
                        wareHouse.setStaffId(postId);
                        wareHouse.setIdentifier("staffWareHouse");
                        wareHouse.setCreateTime(DateUtils.getNowDate());
                        wareHouse.setCreateId(SecurityUtils.getUserId());
                        wareHouse.setCreateName(SecurityUtils.getUsername());
                        staffList.add(wareHouse);
                    }
                }
                //判断存在的岗位，批量启用
                if (exitErpWareHouseList.size() > 0) {
                    erpWareHouseMapper.batchEnableWare(exitErpWareHouseList);
                }

                //对未添加的岗位批量新增
                if (staffList.size() > 0) {
                    batchAddWare(staffList);
                }
            }
            return 1;
        } else {
            erpWareHouse.setWareNo("CK" + UniqueIdUtils.generateUniqueId());
            return erpWareHouseMapper.insertErpWareHouse(erpWareHouse);
        }
    }

    /**
     * 修改仓库管理
     *
     * @param erpWareHouse 仓库管理
     * @return 结果
     */
    @Override
    @Transactional
    public int updateErpWareHouse(ErpWareHouse erpWareHouse) {
        return erpWareHouseMapper.updateErpWareHouse(erpWareHouse);
    }

    /**
     * 批量删除仓库管理
     *
     * @param wareIds 需要删除的仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteErpWareHouseByWareIds(Long[] wareIds) {
        return erpWareHouseMapper.deleteErpWareHouseByWareIds(wareIds);
    }

    /**
     * 删除仓库管理信息
     *
     * @param wareId 仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteErpWareHouseByWareId(Long wareId) {
        return erpWareHouseMapper.deleteErpWareHouseByWareId(wareId);
    }

    @Override
    public Boolean batchAddWare(List<ErpWareHouse> list) {
        erpWareHouseMapper.batchAddWare(list);
        return true;
    }

    @Override
    public int updateWareNameByPostId(ErpWareHouse erpWareHouse) {
        return erpWareHouseMapper.updateWareNameByPostId(erpWareHouse.getWareName(), erpWareHouse.getStaffId());
    }

    @Override
    public int closureWareHouse(Long projectId) {
        return erpWareHouseMapper.closureWareHouse(projectId);
    }

    @Override
    public List<ErpWareCommodityNumber> getCommodityNumberList(ErpWareCommodityNumber erpWareCommodityNumber) {
        List<ErpWareCommodityNumber> list = erpWareCommodityNumberMapper.selectErpWareCommodityNumberList(erpWareCommodityNumber);
        for (ErpWareCommodityNumber wareCommodityNumber : list) {
            wareCommodityNumber.setCommodityType(CommunityTypeConversionUtils.conversion(wareCommodityNumber.getCommodityType()));
        }
        return list;
    }
}
